// base64编码
//获取到从A-Z的字符串： item+index是码点,从码点到字符
let STR = new Array(26).fill(65).map((item, index) =>
    String.fromCodePoint(item + index)).join('')
// 获取到从a-z的字符串：小写a从97开始      
let str = new Array(26).fill(97).map((item, index) =>
    String.fromCodePoint(item + index)).join('')
// 字符集
const base64Code = STR + str + '0123456789+/'
export function toBase64(str: any) {
    let item = '', enCode = '', res = ''
    //码元转为二进制，不足8位的在左侧补
    var code = str.split('').reduce((pre: any, cur: any) =>
        pre + cur.charCodeAt().toString(2).padStart(8, '0'), '')
    //?????
    if (code.length % 24 === 8) {
        code += '0000'//12-8
        res += '=='
    }
    if (code.length % 24 === 16) {
        code += '00'//18-16
        res += '='
    }
    for (let i = 0; i < code.length; i += 6) {
        item = code.slice(i, i + 6)
        enCode += base64Code[parseInt(item, 2)]
    }
    return enCode + res
}